/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.ProductManagement;
import jard.webshop.nbp.Category;
import jard.webshop.nbp.Product;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author CJP
 */
@ManagedBean(name = "productListModel")
@SessionScoped
public class ProductListModel implements Serializable {

    private Category cat = null;
    private List<Product> products = new ArrayList<Product>();
    private final ProductManagement pm;
    private TreeNode selectedNode = new DefaultTreeNode();
    

    /** Creates a new instance of ProductListModel */
    public ProductListModel() {
        pm = ProductManagement.getInstance();
        products = pm.getAllProducts(Boolean.FALSE);
    }
    
    public List<Product> getProducts() {
        return products;
    }

    public void nodeListener(NodeSelectEvent obj) {
        selectedNode = obj.getTreeNode();

        if (selectedNode == null || selectedNode.getData() == null) {
            return;
        } else {
            cat = (Category) selectedNode.getData();
            products = pm.getProductsByCategory(cat);
        }
    }
}
